package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.pojo.Customer;
import com.chixing.pojo.CustomerExample;
import com.chixing.service.RegisterService;
import com.chixing.util.MD5Utils;
import com.chixing.util.RSA.AESUtil;
import com.chixing.util.RSA.RSAUtil;
import com.chixing.util.ServerResponse;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.KeyPair;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.Base64;
import java.util.List;

@Service
public class RegisterServiceImpl implements RegisterService {
    private static KeyPair keyPair;
    private final String key = "EOy6M96s3K1DF3V6JdfBqT3GPtp61ga9";
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public ServerResponse add(Customer customer) {
        Long custTel = customer.getCustTel();
        CustomerExample customerExample = new CustomerExample();
        customerExample.createCriteria().andCustTelEqualTo(custTel);
        List<Customer> customerList = customerMapper.selectByExample(customerExample);
        Customer customer1 = null;
        if (customerList.size() > 0)
            customer1 = customerList.get(0);
        int rows = 0;
        if (customer1 == null) {
//            System.out.println("注册的密码" + customer.getCustPassword());
            String password = MD5Utils.formPassToDBPass(customer.getCustPassword(), "1a2b3c4d");
            customer.setCustPassword(password);
//            System.out.println("最终持久化到数据库中的密码是：" + password);
            rows = customerMapper.insert(customer);
        }
        if (rows > 0) {
            return ServerResponse.saveSuccess(rows);
        } else {
            return ServerResponse.saveFail(null);
        }
    }

    @Override
    public ServerResponse getCustTel(Long custTel) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustTelEqualTo(custTel);
        List<Customer> customerList = customerMapper.selectByExample(example);
        if (customerList.size() > 0) {
            Customer customer = customerList.get(0);
            return ServerResponse.saveSuccess(customer);
        } else {
            return ServerResponse.saveFail(null);
        }
    }

    @Override
    public ServerResponse register(Customer customer) {
        String encrypted = customer.getCustPassword();
        String privateKey = getPrivateKey();
        try {
//            System.out.println("RSA解密中.....");
            String decrypted = RSAUtil.privateDecrypt(encrypted, RSAUtil.getPrivateKey(privateKey));

//            System.out.println("RSA解密后：" + decrypted);


            String encrypt = AESUtil.encrypt(decrypted, key);

//            System.out.println("AES加密后：" + encrypt);
            customer.setCustPassword(encrypt);

        } catch (Exception e) {
            e.printStackTrace();
        }

        int rows = customerMapper.insert(customer);
        if (rows > 0) return ServerResponse.saveSuccess(rows);
        else return ServerResponse.saveFail(rows);
    }

    @Override
    public ServerResponse getPublicKey() {

        try {
            keyPair = RSAUtil.genKeyPair(1024);
            PublicKey publicKey = keyPair.getPublic();
//            System.out.println("公钥：");
            String pubKey = new String(Base64.getEncoder().encode(publicKey.getEncoded()));
//            System.out.println(pubKey);
            return ServerResponse.getSuccess(pubKey);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ServerResponse.getFail(null);

    }

    @Override
    public String getPrivateKey() {
        try {
            PrivateKey privateKey = keyPair.getPrivate();
//            System.out.println("get私钥：");
            String priKey = new String(Base64.getEncoder().encode(privateKey.getEncoded()));


//            System.out.println(priKey);
            return priKey;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ServerResponse Login1(Long custTel, String custPassword) {

        CustomerExample customerExample = new CustomerExample();

        String privateKey = getPrivateKey();

        String encrypt = null;
        try {
//            System.out.println("RSA解密中.....");
            String decrypted = RSAUtil.privateDecrypt(custPassword, RSAUtil.getPrivateKey(privateKey));

//            System.out.println("RSA解密后：" + decrypted);


            encrypt = AESUtil.encrypt(decrypted, key);

//            System.out.println("AES加密后：" + encrypt);

        } catch (Exception e) {
            e.printStackTrace();
        }

        try{
            Long usertrl=Long.parseLong(String.valueOf(custTel));
            customerExample.createCriteria().andCustTelEqualTo(usertrl).andCustPasswordEqualTo(encrypt);
        }catch (NumberFormatException e){
            customerExample.createCriteria().andCustEmailEqualTo(String.valueOf(custTel)).andCustPasswordEqualTo(encrypt);
        }
        List<Customer> customers=customerMapper.selectByExample(customerExample);

        if(customers.size()>0){
            return ServerResponse.getSuccess(customers.get(0));
        }
        return ServerResponse.getFail(null);
    }

}
